package fu.capstone.hanoicityguide.presentation.fragment.nightlife;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.UiLifecycleHelper;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.nightlife.NightLifeBO;
import fu.capstone.hanoicityguide.business.bo.nightlife.NightLifeBOImp;
import fu.capstone.hanoicityguide.business.model.NightLife;
import fu.capstone.hanoicityguide.common.utility.InternetSetting;
import fu.capstone.hanoicityguide.common.utility.SharingFacebook;
import fu.capstone.hanoicityguide.presentation.adapter.ViewPagerAdapter;
import fu.capstone.hanoicityguide.presentation.fragment.home.HomeFragment;

;

@SuppressLint("ValidFragment")
public class DetailNightLifeFragment extends Fragment {

	private NightLifeBO nightlifeBO;
	private int nightlifeID;
	private ViewPager imgFragment;
	private ViewPagerAdapter vpagerAdapter;
	// private TextToSpeech tts;
	private TextView niliDescription;
	private TextView niliInformation;
	private UiLifecycleHelper uiHelper;
	private InternetSetting internetSetting;
	private NightLife nightlife;

	public DetailNightLifeFragment() {

	}

	public DetailNightLifeFragment(int nightlifeID) {
		this.nightlifeID = nightlifeID;
	}

	public int getRestaurantID() {
		return this.nightlifeID;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (uiHelper != null) {
			uiHelper.onDestroy();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		try {
			setHasOptionsMenu(true);
		} catch (Exception e) {
			Log.e("DetailNightLifeFragment", e.toString());
		}
		internetSetting = new InternetSetting();
		// tts = new TextToSpeech(container.getContext(), this);
		// list Restaurant by filter and sorter
		View browseNightLifeFragment = inflater.inflate(
				R.layout.fragment_detail_nightlife, container, false);

		nightlifeBO = new NightLifeBOImp(container.getContext());

		// get data from database
		nightlife = nightlifeBO.browseNightLife(getRestaurantID());

		// fill data into components
		niliDescription = (TextView) browseNightLifeFragment
				.findViewById(R.id.txtNiliDescription);
		niliInformation = (TextView) browseNightLifeFragment
				.findViewById(R.id.txtNiliInformation);

		niliDescription.setText(nightlife.getDescription());

		String information = "";

		if (!"".equals(nightlife.getAddress().trim())) {
			information = information.concat("Address : ");
			information = information.concat(nightlife.getAddress().trim());
			information = information.concat("\n");
		}
		if (!"".equals(nightlife.getPhoneNumber().trim())) {
			information = information.concat("Phone number : ");
			information = information.concat(nightlife.getPhoneNumber().trim());
			information = information.concat("\n");
		}
		if (!"".equals(nightlife.getWebsite().trim())) {
			information = information.concat("Website : ");
			information = information.concat(nightlife.getWebsite().trim());
			information = information.concat("\n");
		}

		if (!"".equals(nightlife.getOpenTime().trim())
				&& !"".equals(nightlife.getOpenTime())) {
			information = information.concat("Officehour : ");
			information = information.concat(nightlife.getOpenTime());
			information = information.concat(" - ");
			information = information.concat(nightlife.getCloseTime());
			information = information.concat("\n");
		}
		niliInformation.setText(information);

		nightlife.getArrImage();

		imgFragment = (ViewPager) browseNightLifeFragment
				.findViewById(R.id.nightlifePager);

		vpagerAdapter = new ViewPagerAdapter(getActivity(),
				nightlife.getArrImage());

		imgFragment.setAdapter(vpagerAdapter);

		return browseNightLifeFragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.detailed_nightlife_page_menu, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Fragment fragment = new NightLifeOnlineMapFragment();
		internetSetting = new InternetSetting(getActivity());
		switch (item.getItemId()) {
		case R.id.action_map: {
			if (internetSetting.getConnectivityStatus(getActivity())) {
				fragment = new NightLifeOnlineMapFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("NightLifeID", nightlifeID);
				fragment.setArguments(bundle);
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container, fragment)
							.commit();
					getActivity().setTitle("View on map");
				}
			}

			if (!internetSetting.getConnectivityStatus(getActivity())
					&& HomeFragment.isPurchased) {
				fragment = new NightLifeOfflineMapFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("NightLifeID", nightlifeID);
				fragment.setArguments(bundle);
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container, fragment)
							.commit();
					getActivity().setTitle("View on map");
				}
			}

			if (!internetSetting.getConnectivityStatus(getActivity())
					&& !HomeFragment.isPurchased) {
				internetSetting.showSettingsAlert();
			}

		}
			break;
		case R.id.action_checkin: {
			if (internetSetting.getConnectivityStatus(getActivity())) {
				uiHelper = new UiLifecycleHelper(getActivity(), null);
				SharingFacebook.shareFacebook(uiHelper, getActivity());
			} else {
				internetSetting.showSettingsAlert();
			}
		}
			break;

		case R.id.action_contact: {
			if (nightlife != null
					&& (!nightlife.getPhoneNumber().trim().equals("") || !nightlife
							.getWebsite().trim().equals(""))) {

				AlertDialog.Builder alertDialog = new AlertDialog.Builder(
						getActivity());

				// Setting Dialog Title
				alertDialog.setTitle("Contact to " + nightlife.getName());

				// Setting Dialog Message
				alertDialog.setMessage("Do you want to contact with this "
						+ nightlife.getType() + " ?");

				if (!nightlife.getPhoneNumber().trim().equals("")) {
					// On pressing Call button
					alertDialog.setPositiveButton("Call",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Intent intent = new Intent(
											Intent.ACTION_CALL);
									intent.setData(Uri.parse("tel:"
											+ nightlife.getPhoneNumber()));
									getActivity().startActivity(intent);
								}
							});
				}

				if (!nightlife.getWebsite().trim().equals("")) {
					alertDialog.setNeutralButton("Go to Website",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Intent intent = new Intent(
											Intent.ACTION_VIEW);
									String link = nightlife.getWebsite();
									if (!link.startsWith("http://")
											&& !link.startsWith("https://")) {
										link = "http://" + link;
									}
									intent.setData(Uri.parse(link));
									startActivity(intent);
								}
							});
				}

				// on pressing cancel button
				alertDialog.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});

				// Showing Alert Message
				alertDialog.show();

			} else {
				Toast.makeText(getActivity(), "No contact information !",
						Toast.LENGTH_SHORT).show();
			}

			break;
		}
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getActivity().getActionBar().setTitle(
				nightlifeBO.browseNightLife(nightlifeID).getName());
	}
}
